package de.fhaugsburg.games.boardgames.scrabble.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Locale;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;

import de.fhaugsburg.games.boardgames.ModelChangedListener;
import de.fhaugsburg.games.boardgames.scrabble.IScrabbleEngine;
import de.fhaugsburg.games.boardgames.scrabble.ModelChangedEvent;
import de.fhaugsburg.games.boardgames.scrabble.gui.listeners.MousePlayListener;




public class ScrabbleMainWindow extends JFrame  {

	//FIXME! bad!
	public static final String IMG = System.getProperty("user.dir") + "/src/resource/images/";

	
	private IScrabbleEngine engine;
	private Controller controller;
	
	private JLabel messageLabel;
	private JPanel allPlayersPanel;
	private Locale locale;
	private Messages messages;
	private GLetterBar letterBar;
	

	private BoardView boardView;


	private Component dragComponent;


	private int dragX;


	private int dragY;
	
	
	
	
	
	public ScrabbleMainWindow(final IScrabbleEngine engine) {
		this.engine = engine;
		this.locale = Locale.getDefault();

		controller = new Controller(engine, this);

		//remove listener from engine, if window is closed
		addWindowListener(
	      new WindowAdapter() {
	        public void windowClosing(WindowEvent e) {
	          engine.removeModelChangedListener(ScrabbleMainWindow.this.controller);
	          ScrabbleMainWindow.this.dispose();
	        }
	      }
	    );
		//now build the arena!
		buildGUI();
		//listener, if something has changed
	    engine.addModelChangedListener(controller);
	    
	    //required for drag and drop between panels
	    MousePlayListener mousePlayListener = new MousePlayListener(controller);
	    this.addMouseListener(mousePlayListener); 
	    this.addMouseMotionListener(mousePlayListener);

		setVisible(true);
	}
	
	
	
	
	/**
	 * builds the window using given locale
	 * @param locale
	 */
	private void buildGUI(){
//		this.removeAll();
		setTitle("Scrabble 2009");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		setResizable(false);
		setSize(630, 560);
		//center on screen
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(screenSize.width/2 - getWidth()/2, screenSize.height/2 - getHeight()/2);

		//Menubar - not needed, for education :)
		JMenuBar menuBar = createMenuBar();
			setJMenuBar(menuBar);
		
		//Toolbar
		add(BorderLayout.NORTH, createToolBar());
		
		//game details on the right
        JPanel infoPanel = new JPanel();
	        infoPanel.setBackground(Color.WHITE);
	        infoPanel.add(createInfoPanel());
	        add(BorderLayout.EAST, infoPanel);
        
        //the scrabbleboard
        JPanel centerStage = new JPanel();
        	centerStage.setLayout(new BorderLayout());
        letterBar = new GLetterBar(engine);
        	centerStage.add(BorderLayout.SOUTH, letterBar);
        boardView = new BoardView(engine.getBoard());
        	centerStage.add(BorderLayout.CENTER, boardView);
        add(BorderLayout.CENTER, centerStage);
        	
        //statusbar
		add(BorderLayout.SOUTH, createStatusBar());
		pack();
	}
	
	
	
	  private JMenuBar createMenuBar() {
	        JMenuBar menuBar = new JMenuBar();

	        // create file menu with submenues and menu items
	        JMenu fileMenu = new JMenu("File");
	        menuBar.add(fileMenu);

	        JMenu openMenu = new JMenu("Open");
	        fileMenu.add(openMenu);
	        JMenuItem menuItem = new JMenuItem("File");
	        openMenu.add(menuItem);
//	        menuItem.addActionListener(new OpenListener(controller));
	        menuItem = new JMenuItem("URL");
	        openMenu.add(menuItem);

	        // menu item with Shortcut <Ctrl> s
	        menuItem = new JMenuItem("Save");
	        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
	        fileMenu.add(menuItem);
//	        menuItem.addActionListener(new SaveListener(controller));

	        fileMenu.add(menuItem = new JMenuItem("Save as ..."));
//	        menuItem.addActionListener(new SaveAsListener(controller));

	        fileMenu.addSeparator();
	        fileMenu.add(menuItem = new JMenuItem("Exit"));
//	        menuItem.addActionListener(new ExitListener(controller));

	        menuBar.add(new JMenu("Edit"));

//	        menuBar.add(createLfMenu());

	        menuBar.add(new JMenu("Help"));

	        return menuBar;
	    }
	  
	  
	
	private JPanel createInfoPanel(){
		JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new GridLayout(0, 1));
        infoPanel.setBackground(Color.WHITE);
        
        infoPanel.add(createActivePlayersPanel());
        
        return infoPanel;
	}
	
	
	
	/**
	 * panel on the right, display all active players, scores and additional game info
	 * @return
	 */
	private JPanel createActivePlayersPanel(){
		JPanel playerPanel = new JPanel();
		playerPanel.setLayout(new GridLayout(0, 1));
		playerPanel.setBackground(Color.WHITE);
		
		playerPanel.add(new JLabel("Players:"));
		
		allPlayersPanel = new JPanel();
		allPlayersPanel.add(new JLabel("please start new game"));
			
		playerPanel.add(allPlayersPanel);
		
		return playerPanel;		
	}
	
	
	
	/**
	 * statusbar at the bottom, display messages
	 * @return
	 */
	private JPanel createStatusBar(){
		JStatusBar statusBar = new JStatusBar();
		messageLabel = new JLabel("Scrabble 2009 - Thomas Triffo, Klaus Göttling");
        //messageLabel.setFont(new Font("Arial", Font.ITALIC, 10));
        statusBar.add(BorderLayout.WEST, messageLabel);
        return statusBar;
	}
	
	
	/**
	 * build the toolbar
	 * @return
	 */
	private JToolBar createToolBar() {
        JToolBar toolbar = new JToolBar();

        JButton newgameButton = new JButton(new ImageIcon(IMG + "icon_newgame.png"));
        	newgameButton.setToolTipText("new game");
        	newgameButton.setText("new game");
        	newgameButton.addActionListener(
        		new ActionListener() {
        			public void actionPerformed(ActionEvent e) {
        				ScrabbleMainWindow.this.controller.newGameDialog();
        			}
        		}
        		);
        	
        toolbar.add(newgameButton);
        return toolbar;
    }
	
	
	
	
	/**
	 * initialze new game
	 */
	public void displayNewGameDialog(){
		NewgameDialog newgameDialog = new NewgameDialog(controller);
	}
	
	
	
	/**
	 * redraws the GUI
	 */
	public void reDraw(){
		if (engine.getActivePlayer() != null){
			letterBar.showPieces(engine.getActivePlayer().getMyPieces());
		}
		letterBar.repaint();
		boardView.updateBoardView();
		boardView.repaint();
		pack();
	}
	
	
	
	
	/**
	 * sets new statusmessage
	 * @param message
	 */
	public void setStatusMessage(String message) {
		messageLabel.setText(message);
	}


	
	/**
	 * displays error-message dialog
	 * @param title
	 * @param message
	 */
	public void errorMessage(String title, String message){
		JOptionPane.showMessageDialog(this, message, title, JOptionPane.ERROR_MESSAGE);
	}

	
	
}
